package dao

import (
	"errors"
	"fmt"
	__ "user-srv/basic/proto"
	"user-srv/handler/models"
)

// 站内信添加
func CreateAppInfo(in *__.InAppInfoAddReq) (uint32, error) {
	var i models.InAppChat
	i = models.InAppChat{
		UserId:   in.UserId,
		ToUserId: in.ToUserId,
		Title:    in.Title,
		Content:  in.Content,
		Types:    uint8(in.Types),
	}
	err := i.CreateInAppMessage()
	if err != nil {
		return 0, errors.New("添加失败")
	}
	return i.Id, err
}

// 站内信列表
func InAppInfoList(in *__.InAppInfoListReq) ([]*__.InAppInfoList, error) {
	var i models.InAppChat
	i.ToUserId = in.ToUserId
	list, err := i.GetInfoList()
	if err != nil {
		return nil, errors.New("查询失败" + err.Error())
	}
	var InfoList []*__.InAppInfoList
	for _, v := range list {
		l := __.InAppInfoList{
			Id:       int64(v.Id),
			ToUserId: v.ToUserId,
			Title:    v.Title,
			Content:  v.Content,
			Types:    int64(v.Types),
			IsRead:   uint32(v.IsRead),
		}
		InfoList = append(InfoList, &l)
	}
	return InfoList, nil
}

// 站内信详情
func InAppInfoDetail(in *__.InAppInfoDetailReq) (models.InAppChat, error) {
	var i models.InAppChat
	i.Id = in.Id

	if err := i.InfoDetail(); err != nil {
		return models.InAppChat{}, errors.New("查询失败" + err.Error())
	}
	i.IsRead = 2
	err := InAppInfoUpdate(i)
	if err != nil {
		fmt.Errorf("修改失败" + err.Error())
	}
	return i, nil
}

// 修改站内信
func InAppInfoUpdate(i models.InAppChat) error {
	if err := i.UpdateInfo(); err != nil {
		return errors.New("修改站内信失败" + err.Error())
	}
	return nil
}
func UpdateIsRead(id int64) error {
	var i models.InAppChat
	i.Id = uint32(id)
	i.IsRead = 2
	if err := i.UpdateInfo(); err != nil {
		return errors.New("修改站内信失败" + err.Error())
	}
	return nil
}
